<?php

namespace App\Admin\Controllers;


use App\Admin\Repositories\IntegralRecord;
use App\Models\AgricGoods;
use App\Models\IntegralRule;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class IntegralRecordController extends AdminController
{
    protected $user_id = '';
    public function __construct()
    {

    }

    public function index(Content $content)
    {

        return $content
            ->title('积分管理')
            ->description('列表')
            ->body(function (Row $row) {
                $row->column(12, $this->grid());
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $user_id = $_GET['user_id'];
        return Grid::make(new IntegralRecord(), function (Grid $grid)use($user_id) {

            $grid ->model() -> where('user_id',$user_id)-> orderBy('id','desc');


            $grid->column('id')->sortable();
            $grid->column('integral_num')-> display(function($integral_num){
                return ($this->types == 1 ? '+' : '-') . $integral_num;
            });
            $grid->column('integral_num_after');
            $grid->column('types') -> display(function($types){
                return $types == 1 ? '收入' : '支出';
            });
//            $grid->column('obj_source') -> display(function($obj_source){
//                return $obj_source == 1 ? '邀请' : '商品详情页领取';
//            });
//            $grid->column('obj_id') -> display(function($obj_id){
//
//                $obj_source = $this -> obj_source;
//                if($obj_source == 1){
//                    $user = User::find($obj_id);
//                    return $user ? ($user->real_name ?: $user->user_name) : '';
//                }else if($obj_source == 2){
//                    $goods = AgricGoods::find($obj_id);
//                    return $goods ? ($goods->goods_name ?: '') : '';
//                }
//            });
            $grid->column('explain')->width('300px');

            $grid->column('created_at', '记录时间') ->width('200px');

            Form::dialog('编辑')
                ->click('.edit-form')
                ->success('Dcat.reload()');


            /*$grid->model()->setConstraints([
                'id' => $this -> user_id,
            ]);*/

            $grid->actions(function($actions){
                // 获取当前行主键值
                $id = $actions->getKey();
                $editPage = admin_base_path('users/integral_records/'.$id.'/edit?id='.$id);

                $actions->append("<a style='cursor: pointer' class='grid-row-action-620b3abca5910' href='$editPage'><i class='feather icon-edit-1'></i> 编辑</a>");
            });


            $grid->selector(function(Grid\Tools\Selector $selector){
                $selector->selectOne('types','类型',['1' => '收入','2' => '支出']);
            });

            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new IntegralRecord(), function (Show $show) {
            $show->field('id');
            $show->field('integral_num');
            $show->field('types');
            $show->field('obj_source');
            $show->field('obj_id');
            $show->field('explain');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }


    public function edit($id, Content $content)
    {
        return $content
            ->title('积分管理')
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form($id)->edit($id));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id=0)
    {
        return Form::make(new IntegralRecord(), function (Form $form)use($id) {
            $integralRecord = $form ->model() ->where('id',$id) -> first();
            $user_id = $integralRecord -> user_id;


            $form->display('id');
            $form->text('integral_num');
            $form->radio('types') ->options(['1' => '收入','2' => '支出'])->disable();

            /*$form->radio('obj_source')->when(1,function(Form $form){

                if($form ->model() ->id){
                    $user = User::find($form ->model() -> obj_id);
                    $form->text('user_name','邀请的用户名')->value($user ? ($user->real_name ?: $user->user_name) : '')->disable();
                }
            })->when(2,function(Form $form){

                if($form ->model() ->id){
                    $goods = AgricGoods::find($form ->model() -> obj_id);
                    $form->text('obj_id','商品ID')->disable();
                    $form->text('goods_name','商品名称') ->value($goods ? ($goods->goods_name ?: '') : '')->disable();
                }
            })->options(['1' => '邀请', '2' => '商品详情页领取'])->disable();*/

            $form->text('explain');
        
            $form->display('created_at');
            $form->display('updated_at');


            $form->tools(function(Form\Tools $tools)use($user_id){

                $listPage = admin_base_path('users/integral_records?user_id='.$user_id);
                $tools ->disableList();
                $tools ->append("<a href='$listPage' class='btn btn-sm btn-white'><i class='feather icon-list'></i><span class='d-none d-sm-inline'>&nbsp;列表</span></a>");
            });

            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableResetButton();

        });
    }
}
